package cn.zyjblogs.server.role.controller;

import cn.zyjblogs.server.role.service.RolePermissionRelService;
import cn.zyjblogs.server.role.vo.RolePermissionRelVo;
import cn.zyjblogs.starter.common.entity.response.ResponseObject;
import cn.zyjblogs.starter.common.entity.response.ResponseResult;
import cn.zyjblogs.starter.web.apiversion.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;

/**
 * @author zhuyijun
 */
@Api(value = "角色权限关联控制层")
@RestController
@RequestMapping("/{v}/role/rel")
@ApiVersion(1)
@ResponseBody
@RequiredArgsConstructor
public class RolePermissionRelController {
    private final RolePermissionRelService rolePermissionRelService;

    /**
     * @param roleId 角色id
     * @author zhuyijun
     * @date 2022/9/24 上午11:56
     * return 权限id
     */
    @ApiOperation(value = "通过角色id查询权限集合", notes = "通过角色id查询权限集合")
    @ApiVersion(1)
    @GetMapping("/findListByRoleId")
    public ResponseObject<List<String>> findListByRoleId(String roleId) {
        return ResponseResult.success(rolePermissionRelService.findListByRoleId(roleId));
    }

    /**
     * @param roleIds 角色id
     * @author zhuyijun
     * @date 2022/9/24 上午11:56
     * return 权限id
     */
    @ApiOperation(value = "通过角色id集合查询权限id集合", notes = "通过角色id集合查询权限id集合")
    @ApiVersion(1)
    @PostMapping("/findListByRoleIds")
    public ResponseObject<List<String>> findListByRoleIds(List<String> roleIds) {
        return ResponseResult.success(rolePermissionRelService.findListByRoleIds(roleIds));
    }

    /**
     * @param roleId 角色id
     * @return 角色和权限关联表
     * @author zhuyijun
     * @date 2022/9/24 上午11:56
     */
    @ApiOperation(value = "通过角色id查询关联对象集合", notes = "通过角色id查询关联对象集合")
    @ApiVersion(1)
    @GetMapping("/findListVoByRoleId")
    public ResponseObject<List<RolePermissionRelVo>> findListVoByRoleId(String roleId) {
        return ResponseResult.success(rolePermissionRelService.findListVoByRoleId(roleId));
    }

    /**
     * @param roleIds 角色id
     * @return 角色和权限关联表
     * @author zhuyijun
     * @date 2022/9/24 上午11:56
     */
    @ApiOperation(value = "通过角色id集合查询关联对象集合", notes = "通过角色id集合查询关联对象集合")
    @ApiVersion(1)
    @PostMapping("/findListVoByRoleIds")
    public ResponseObject<List<RolePermissionRelVo>> findListVoByRoleIds(List<String> roleIds) {
        return ResponseResult.success(rolePermissionRelService.findListVoByRoleIds(roleIds));
    }

    /**
     * 通过角色id删除权限
     *
     * @param roleId
     * @return
     * @author zhuyijun
     * @date 2022/9/24
     */
    @ApiOperation(value = "通过角色id删除角色", notes = "通过角色id删除角色")
    @ApiVersion(1)
    @PostMapping("/deleteByRoleId")
    public ResponseObject<Boolean> deleteByRoleId(String roleId) {
        return ResponseResult.success(rolePermissionRelService.deleteByRoleId(roleId));
    }

    /**
     * 通过角色id删除权限
     *
     * @param roleIds
     * @return
     * @author zhuyijun
     * @date 2022/9/24
     */
    @ApiOperation(value = "通过角色id集合删除角色", notes = "通过角色id集合删除角色")
    @ApiVersion(1)
    @PostMapping("/deleteByRoleIds")
    public ResponseObject<Boolean> deleteByRoleIds(List<String> roleIds) {
        return ResponseResult.success(rolePermissionRelService.deleteByRoleIds(roleIds));
    }

    /**
     * 保存或更新角色权限关联
     *
     * @param roleId
     * @param permissionIds
     * @return
     * @author zhuyijun
     * @date 2022/9/24
     */
    @ApiOperation(value = "角色权限保存", notes = "角色权限关联保存")
    @ApiVersion(1)
    @PutMapping("/save")
    public ResponseObject<Boolean> save(String roleId, Collection<String> permissionIds) {
        return ResponseResult.success(rolePermissionRelService.saveUpdate(roleId, permissionIds, false));
    }


    /**
     * 保存或更新角色权限关联
     *
     * @param roleId
     * @param permissionIds
     * @return
     * @author zhuyijun
     * @date 2022/9/24
     */
    @ApiOperation(value = "角色权限更新", notes = "角色权限更新")
    @ApiVersion(1)
    @PutMapping("/update")
    public ResponseObject<Boolean> update(String roleId, Collection<String> permissionIds) {
        return ResponseResult.success(rolePermissionRelService.saveUpdate(roleId, permissionIds, false));
    }
}
